<?php
namespace app\api\controller;
use think\Db;
use think\Session;
use think\Controller;

class Test extends  Controller
{
	/**
	*测试投票(供压力测试)
	*
	*/
	//
	public function test_vote()
	{
		if(request()->isGet())
		{
			$id = $_GET['id'];
			$name = $_GET['name'];
			$account = $_GET['account'];
			$sign = $_GET['sign'];

			$res = Db::name('candidate')->where('id',$id)->setInc($sign); //一次更新多行，返回更新的行数
			//投票成功
			if($res >= 0) 
			{
				//插入投票人表
				$voter['account'] = $account;
				//$voter['password'] = Session::get('password','think');
				$voter['status'] = $sign;
				$in_voter = db('voter',[],false)->insert($voter);
				if(!$in_voter)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}
				//插入投票表
				$toupiao['name'] = $name;
				$toupiao['prize_name'] = prize_name($sign);
				$toupiao['account'] = $account;
				$toupiao['time'] = time();
				$in_vote = db('vote',[],false)->insert($toupiao);
				if(!$in_vote)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}

				$info['code'] = 0;
	        	$info['message'] = '投票成功';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}


		}
	}



}
